#-*- coding: utf-8 -*-
#! /usr/bin/python

import time
import win32api
import win32con
import ConfigParser
import hashlib
import datetime


def initConfig(filename):
    '''
    去掉文件头BOM等
    '''
    import re
    content = open(filename).read()
    content = re.sub(r"\xfe\xff", "", content)
    content = re.sub(r"\xff\xfe", "", content)
    content = re.sub(r"\xef\xbb\xbf", "", content)
    open(filename, 'w').write(content)


def logit(content):
    '''
    另一种log记录函数
    '''
    file = open('error.txt', 'a')
    try:
        timestr = datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')+': '
        file.write(timestr+content+'\r\n')
    finally:
        file.close()


def ordd(str):
    '''
    解决键盘码不对应问题,不知道别的电脑有没有这个问题
    '''
    t = ord(str)
    #65到90是A到Z
    if(t>90):
        return t-32
    else:
        return t


def pressMe():
    '''
    模拟人工摇
    '''
    win32api.keybd_event(37, 0, 0, 0)#left
    time.sleep(4)
    win32api.keybd_event(37, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(0.5)
    win32api.keybd_event(39, 0, 0, 0)#right
    time.sleep(1)
    win32api.keybd_event(39, 0, win32con.KEYEVENTF_KEYUP, 0)

def getMacAdd():
    '''
    获得mac地址
    '''
    import uuid
    node = uuid.getnode()
    mac = uuid.UUID(int = node).hex[-12:]
    return mac


def makesn():
    '''
    id是微信号，中英文都可以
    '''
    MAC = getMacAdd()
    x = hashlib.sha224((hashlib.md5('jhf'+MAC)).hexdigest()+'jhfaixiaobao')
    return x.hexdigest()

#读取配置文件
config = ConfigParser.ConfigParser()
initConfig('shake.ini')
config.read('shake.ini')
sn = config.get("global", "sn")
id = config.get("global", "id")
wtime = config.getint("global", "time")
if(not makesn()==sn):
    print 'SN Error!!'
    logit('SN Error!!')
    exit(0)
while 1:
    pressMe()
    print '\r\n OK'
    time.sleep(wtime)
